package com.aps.util;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aps.entity.domain.schedule.ScheduleConfig;
import com.aps.service.rep.ScheduleConfigRep;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liaoguangan
 * @description <排程过程所需>
 * @date 2018/4/1 16:23
 */

@Slf4j
@Component
public class ScheduleComponent {

    //是否马上排程
    private Boolean scheduleSoon;

    //是否正在排程
    private Boolean scheduling;

    //下次排程时间
    private Date nextScheduleTime;

    //全局排程配置
    private ScheduleConfig config;

    //缓存排程队列
    private CopyOnWriteArrayList<String> schedules = new CopyOnWriteArrayList<>();

    @Autowired
    private ScheduleConfigRep scheduleConfigRep;

    public void loadConfig() {
        List<ScheduleConfig> cfgs = scheduleConfigRep.findAll();
        if (null == cfgs || cfgs.size() == 0) {
            log.error("schedule config is null!");
        } else {
            this.setConfig(cfgs.get(0));
        }
    }

    public void removeSchedule(String orderNum) {
        schedules.remove(orderNum);
    }

    public void addSchedule(String orderNum) {
        if (!schedules.contains(orderNum)) {
            schedules.add(orderNum);
        }
    }

    public Integer getMaxValue() {
        return this.schedules.size();
    }

    public ScheduleConfig getConfig() {
        return config;
    }

    public void setConfig(ScheduleConfig config) {
        this.config = config;
    }

    public Boolean getScheduleSoon() {
        return scheduleSoon;
    }

    public void setScheduleSoon(Boolean scheduleSoon) {
        this.scheduleSoon = scheduleSoon;
    }

    public Boolean getScheduling() {
        return scheduling;
    }

    public void setScheduling(Boolean scheduling) {
        this.scheduling = scheduling;
    }

    public Date getNextScheduleTime() {
        return nextScheduleTime;
    }

    public void setNextScheduleTime(Date nextScheduleTime) {
        this.nextScheduleTime = nextScheduleTime;
    }
}
